<template>
  <!-- 购买商品组件 -->
  <div class="dettali">
    <!-- 商品头部信息 -->
    <productHeader></productHeader>
    <!-- 用户是否登入 -->
    <div class="login" v-if="!isShowLogin">
      <div class="container">
        <span>为方便您购买，请提前登录</span>
        <router-link to="/login">立即登入</router-link>
        <i @click="isShowLogin=true"></i>
      </div>
    </div>
    <!-- 产品轮播图部分 产品介绍和参数选择 -->
    <div class="product-con">
      <div class="container">
        <div class="product-foucs">
          <div class="swiper-box">
            <swiper :options="swiperOption">
              <!-- slides -->
              <swiper-slide><img src="imgs/detail/phone-1.jpg" alt=""></swiper-slide>
              <swiper-slide><img src="imgs/detail/phone-2.jpg" alt=""></swiper-slide>
              <swiper-slide><img src="imgs/detail/phone-3.jpg" alt=""></swiper-slide>
              <swiper-slide><img src="imgs/detail/phone-4.jpg" alt=""></swiper-slide>
              <div class="swiper-button-prev" slot="button-prev"></div>
              <div class="swiper-button-next" slot="button-next"></div>
            </swiper>
          </div>
        </div>
        <div class="product-msg">
          <!-- 产品介绍 -->
          <div class="ask">
            <h2>{{productAttr.title}}</h2>
            <p class="color">{{productAttr.sale}}</p>
            <p>{{productAttr.text}}</p>
            <p class="price color">{{productAttr.price}}￥</p>
          </div>
          <!-- 版本 -->
          <div class="relese">
            <div>
              <h3>{{banben.name || ''}}</h3>
              <ul>
                <li v-for="(child,index) in banben.list" :key="index" :class="{'isSelct':child.isDefault}" @click="changeSelct(banben.list,child,1)">{{child.value}}</li>
              </ul>
            </div>
            <div>
              <h3>{{color.name || ''}}</h3>
              <ul>
                <li v-for="(child,index) in color.list" :key="index" :class="{'isSelct':child.isDefault}" @click="changeSelct(color.list,child,2)">{{child.value}}</li>
              </ul>
            </div>
          </div>
          <!-- 选中产品和价格 -->
          <div class="end">
            <p>{{productBanben||'16GB+256GB'}} {{productColor|| '黑色'}}</p>
            <div class="price">总价：2399￥</div>
          </div>
          <!-- 结算 -->
          <div class="buy">
            <a href="javascript:;" @click="addShopCat">添加到购物车</a>
            <a href="javascript:;">喜欢</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
  name: 'dettail',
  data() {
    return {
      isShowLogin: true,
      // 商品数据
      productAttr: {},
      // 商品型号
      productBanben: '',
      // 商品颜色
      productColor: '',
      // swiper轮播图的配置
      swiperOption: {
        loop: true,
        effect: 'fade',
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  },
  components: {
    swiper,
    swiperSlide
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await this.$API.dettaitList(this.$route.params.id)
      this.isShowLogin = res.isLogin
      this.productAttr = res.data
      console.log(res)
    },
    changeSelct(arr, item, num) {
      if (num === 1) {
        arr.forEach(item => {
          item.isDefault = false
        })
        item.isDefault = true
        this.productBanben = item.value
      } else if (num === 2) {
        arr.forEach(item => {
          item.isDefault = false
        })
        item.isDefault = true
        this.productColor = item.value
      }
    },
    async addShopCat() {
      try {
        await this.$store.dispatch('getAddShopCat', this.$route.params.id)
        this.$message({
          message: '添加到购物车成功',
          type: 'success'
        })
        this.$router.push('/cart')
      } catch (error) {}
    }
  },
  computed: {
    banben() {
      return this.productAttr.banben || {}
    },
    color() {
      return this.productAttr.color || {}
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 48px;
  line-height: 48px;
  background-color: #f7f7f7;
  text-align: center;
  font-size: 16px;
  .container {
    span {
      margin-right: 20px;
    }
    a {
      color: #ff6600;
    }
    i {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin-left: 10px;
      background: url('../../../public/imgs/icon-close.png') no-repeat center;
      background-size: cover;
      vertical-align: middle;
      cursor: pointer;
    }
  }
}
.product-con {
  margin-top: 32px;
  .container {
    overflow: hidden;
  }
  .product-foucs {
    position: relative;
    float: left;
    width: 600px;
    height: 600px;
    .swiper-box {
      position: absolute;
      top: 0;
      left: 20px;
      width: 560px;
      height: 560px;
      img {
        width: 560px;
        height: 560px;
      }
    }
  }
  .product-msg {
    margin-left: 20px;
    float: left;
    .ask {
      padding-bottom: 45px;
      border-bottom: 1px solid #e0e0e0;
      h2 {
        font-size: 26px;
      }
      p {
        font-size: 12px;
        margin-top: 15px;
      }
      .price {
        font-size: 20px;
      }
    }
    .relese {
      width: 600px;
      margin-top: 30px;
      h3 {
        font-size: 22px;
        margin: 20px 0 10px 0;
      }
      ul {
        overflow: hidden;
        li {
          float: left;
          width: 292px;
          height: 42px;
          text-align: center;
          line-height: 42px;
          border: 1px solid #e0e0e0;
          font-size: 18px;
          color: #e0e0e0;
          cursor: pointer;
          margin: 10px 10px 0 0;
          &:nth-child(even) {
            margin-right: 0;
          }
          &:hover {
            border: 1px solid #ff6600;
            color: #ff6600;
          }
        }
        .isSelct {
          border: 1px solid #ff6600;
          color: #ff6600;
        }
      }
    }
    .end {
      width: 540px;
      height: 100px;
      padding: 30px;
      margin-top: 30px;
      background: #f9f9fa;
      p {
        font-size: 14px;
      }
      .price {
        color: #ff6600;
        font-size: 26px;
        line-height: 100px;
      }
    }
    .buy {
      margin-top: 30px;
      a {
        display: inline-block;
        height: 54px;
        text-align: center;
        line-height: 54px;
        font-size: 18px;
        box-sizing: border-box;
        &:first-child {
          width: 300px;
          border: 1px solid #ff6600;
          color: #ff6600;
        }
        &:last-child {
          width: 120px;
          background-color: #e0e0e0;
          border: 1px solid #e0e0e0;
          margin-left: 20px;
          color: #fff;
        }
      }
    }
  }
}
.color {
  color: #ff6600;
}
</style>
